import fetch from '../../services/api';

export const actionTypes = {
    REQUEST: 'REQUEST',
    NEWSTYPE_SUCCESS: 'NEWSTYPE_SUCCESS',
    NEWSLIST_SUCCESS: 'NEWSLIST_SUCCESS',
    ERROR: 'ERROR'
};
//reudx-thunk
const actions = {
    fetchNewsType: () => async dispatch => {
        try {
            dispatch({ type: actionTypes.REQUEST });
            const data = await fetch.newsType();
            if (!data.code) throw data.msg;
            dispatch({
                type: actionTypes.NEWSTYPE_SUCCESS,
                payload: data.data
            });
        } catch (error) {
            dispatch({ type: actionTypes.ERROR, error });
        }
    },
    fetchNewsList: (id, page) => async dispatch => {
        try {
            dispatch({ type: actionTypes.REQUEST });
            const data = await fetch.newsList(id, page);
            if (!data.code) throw data.msg;
            dispatch({
                type: actionTypes.NEWSLIST_SUCCESS,
                payload: data.data
            });
        } catch (error) {
            dispatch({ type: actionTypes.ERROR, error });
        }
    }
};
export default actions;
